import com.hyl.Application;
import com.hyl.DTO.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.io.Reader;

/**
 * Mybatis关联查询映射测试类
 */
@SpringBootTest(classes = Application.class)
public class OnetoManyTester {

    @Test
    public  void findUserWithOrders() {
        //读取文件名
        String resources = "mybatis-config.xml";
        //创建流
        Reader reader = null;
        try {
            //读取mybatis-config.xml文件内容到reader对象中
            reader = Resources.getResourceAsReader(resources);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //初始化mybatis数据库,创建SqlSessionFactory类的实例
        SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
        //创建SqlSession实例
        SqlSession sqlSession = sqlMapper.openSession();
        // 2、查询id为1的用户信息
        User user = sqlSession.selectOne("com.hyl.mapper." + "UserMapper.findUserWithOrders", 1);
        // 3、输出查询结果信息
        System.out.println(user);
        // 4、关闭SqlSession
        sqlSession.close();
    }

    @Test
    public  void findUserWithOrders2() {
        //读取文件名
        String resources = "mybatis-config.xml";
        //创建流
        Reader reader = null;
        try {
            //读取mybatis-config.xml文件内容到reader对象中
            reader = Resources.getResourceAsReader(resources);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //初始化mybatis数据库,创建SqlSessionFactory类的实例
        SqlSessionFactory sqlMapper = new
                SqlSessionFactoryBuilder().build(reader);
        //创建SqlSession实例
        SqlSession sqlSession = sqlMapper.openSession();
        // 2、查询id为1的用户信息
        User user = sqlSession.selectOne("com.hyl.mapper." + "UserMapper.findUserWithOrders2", 1);
        // 3、输出查询结果信息
        System.out.println(user);
        // 4、关闭SqlSession
        sqlSession.close();
    }


}